#include "head.h"


//文件描述符做下标的数组


int add_wheel(int net_fd, List *list, net_node *netfd_num)
{
    // printf("添加开始\n");
    LOG(INFO, "null", "add_wheel", "添加开始");
    netfd_num[net_fd].net_fd = net_fd;
    netfd_num[net_fd].linked_count += 1;
    
    // printf("%d\n", netfd_num[net_fd].linked_count);
    char msg[1024] = {0};
    sprintf(msg, "netfd_num[net_fd].linked_count: %d", netfd_num[net_fd].linked_count);
    LOG(INFO, "null", "add_wheel", msg);
    //头插法插入新结点
    wheel_node *new_node =(wheel_node*)malloc(sizeof(wheel_node));
    new_node->net_id = &netfd_num[net_fd];
    new_node->next = NULL;
    
    if(list->head == NULL)
    {
        list->head = new_node;
        // printf("添加结束\n");
        LOG(INFO, "null", "add_wheel", "添加结束");
        return 0;
    }
    wheel_node* tmp = list->head->next;
    list->head = new_node;
    new_node->next = tmp;

    // printf("添加结束\n");
    LOG(INFO, "null", "add_wheel", "添加结束");
    return 0;
}


int del_wheel(List *list)
{
    while(list->head != NULL)
    {
        wheel_node *curr = list->head;
        list->head = curr->next;
        // printf("net_fd:%d,link_count:%d\n", curr->net_id->net_fd, curr->net_id->linked_count);
        char msg2[1024] = {0};
        sprintf(msg2, "net_fd:%d,link_count:%d", curr->net_id->net_fd, curr->net_id->linked_count);
        LOG(INFO, "null", "del_wheel", msg2);
        if(curr->net_id->linked_count == 1)
        {
            curr->net_id->linked_count -= 1;
            response_t res;
            res.status = 2;
            send(curr->net_id->net_fd, &res, sizeof(response_t), 0);
            close(curr->net_id->net_fd);
            // printf("踢出：%d\n", curr->net_id->linked_count);
            char msg3[1024] = {0};
            sprintf(msg3, "踢出：%d", curr->net_id->linked_count);
            LOG(INFO, "null", "del_wheel", msg3);
            // printf("正常执行\n");
        }
        else if(curr->net_id->linked_count > 1)
        {
            curr->net_id->linked_count -= 1;
        }
        free(curr);
    }
    printf("正常执行\n");
    // LOG(INFO, "null", "del_wheel", "正常执行完毕");
    return 0;
}
